//
//  RegisterThirdViewController.m
//  1243
//
//  Created by 我的 on 16/4/6.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "RegisterThirdViewController.h"
#import "CaptchaView.h"

@interface RegisterThirdViewController ()<UITextFieldDelegate, UIAlertViewDelegate>
{
    CaptchaView *authCodeView;
    UITextField *_input;
}
@property (weak, nonatomic) IBOutlet UITextField *authTextField;
@property (weak, nonatomic) IBOutlet UIView *authView;

@end

@implementation RegisterThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"注册";
    
    //显示验证码界面
    authCodeView = [[CaptchaView alloc] initWithFrame:CGRectMake(0, 0, self.authView.bounds.size.width, self.authView.bounds.size.height)];
    [self.authView addSubview:authCodeView];
    
    //添加输入框
    self.authTextField.placeholder = @"请输入验证码!";
    self.authTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.authTextField.backgroundColor = [UIColor clearColor];
    self.authTextField.returnKeyType = UIReturnKeyDone;
    self.authTextField.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 输入框代理，点击return 按钮
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //判断输入的是否为验证图片中显示的验证码
    if ([self.authTextField.text isEqualToString:authCodeView.authCodeStr])
    {
        //正确弹出警告款提示正确
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"恭喜您 ^o^" message:@"验证成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alview show];
    }
    else
    {
        //验证不匹配，验证码和输入框抖动
        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
        anim.repeatCount = 1;
        anim.values = @[@-20,@20,@-20];
        //        [authCodeView.layer addAnimation:anim forKey:nil];
        [self.authTextField.layer addAnimation:anim forKey:nil];
    }
    
    return YES;
}

#pragma mark 警告框中方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //清空输入框内容，收回键盘
    if (buttonIndex==0)
    {
        self.authTextField.text = @"";
        [self.authTextField resignFirstResponder];
    }
}

- (IBAction)finishRegisterBtn:(UIButton *)sender {
    //判断输入的是否为验证图片中显示的验证码
    if ([self.authTextField.text isEqualToString:authCodeView.authCodeStr])
    {
        //正确弹出警告款提示正确
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"恭喜您 ^o^" message:@"验证成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alview show];
    }
    else
    {
        //验证不匹配，验证码和输入框抖动
        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
        anim.repeatCount = 1;
        anim.values = @[@-20,@20,@-20];
        //        [authCodeView.layer addAnimation:anim forKey:nil];
        [self.authTextField.layer addAnimation:anim forKey:nil];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
